﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace PPWMS.View
{
    public partial class COMOption : Form
    {
        private int _OperationType = 0; // 0:OK; 1:Cancel
        string _CfgFileName = "";

        public COMOption()
        {
            InitializeComponent();
        }

        public int GetOperationType()
        {
            return _OperationType;
        }

        public string GetCfgFileName()
        {
            return _CfgFileName;
        }

        public void SetCfgFileName(string CfgFileName)
        {
            _CfgFileName = CfgFileName;
        }

        public void SetOperationType(int OperationType)
        {
            _OperationType = OperationType;
        }

        #region   保存串口配置信息
        /// <summary>
        /// 保存串口配置信息
        /// </summary>
        /// <returns></returns>
        public void SaveOptions(string CfgFileName)
        {
            if (CfgFileName.Substring(CfgFileName.Length - 4, 4) != ".xml"
                && CfgFileName.Substring(CfgFileName.Length - 4, 4) != ".XML")
            {
                MessageBox.Show("无效的配置文件名！", "提示对话框",
                       MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            XmlDocument doc = new XmlDocument();
            XmlElement LabelOptns = doc.CreateElement("ComOption");

            LabelOptns.SetAttribute("PortNumber", this.comboBoxPortNum.Text);
            LabelOptns.SetAttribute("BaudRate", this.comboBoxBaudRate.Text);
            LabelOptns.SetAttribute("DataBit", this.comboBoxDataBit.Text);
            LabelOptns.SetAttribute("Parity", this.comboBoxParity.Text);
            LabelOptns.SetAttribute("StopBit", this.comboBoxStopBit.Text);
            LabelOptns.SetAttribute("Control", this.comboBoxCtrlBit.Text);

            doc.AppendChild(LabelOptns);
            doc.Save(CfgFileName);//保存。
        }
        #endregion

        #region   读取串口配置信息
        /// <summary>
        ///  读取串口配置信息
        /// </summary>
        /// <returns></returns>
        public void ReadOptions(string CfgFileName)
        {

            if (CfgFileName.Substring(CfgFileName.Length - 4, 4) != ".xml"
                && CfgFileName.Substring(CfgFileName.Length - 4, 4) != ".XML")
            {
                MessageBox.Show("无效的配置文件名！", "提示对话框",
                       MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            XElement LabelOptns = XElement.Load(CfgFileName);
            this.comboBoxPortNum.Text = LabelOptns.Attribute("PortNumber").Value;
            this.comboBoxBaudRate.Text = LabelOptns.Attribute("BaudRate").Value;
            this.comboBoxDataBit.Text = LabelOptns.Attribute("DataBit").Value;
            this.comboBoxParity.Text = LabelOptns.Attribute("Parity").Value;
            this.comboBoxStopBit.Text = LabelOptns.Attribute("StopBit").Value;
            this.comboBoxCtrlBit.Text = LabelOptns.Attribute("Control").Value;

        }
        #endregion

        private void COMOption_Load(object sender, EventArgs e)
        {
            ReadOptions(_CfgFileName);
        }

        private void btnSetCOMOptionOK_Click(object sender, EventArgs e)
        {
            SaveOptions(_CfgFileName);
            _OperationType = 0;
            this.Close();
        }

        private void btnSetCOMOptionCancel_Click(object sender, EventArgs e)
        {
            _OperationType = 1;
            this.Close();
        }
    }
}
